<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;
use app\common\model\System as SystemModel;

class Service extends AdminBase
{
	protected $model;
    protected function _initialize()
    {
        parent::_initialize();
        $this->model = new SystemModel();
    }

    public function ota()
    {
        $ota_info = '';
        $res = Db::name('system')->where('name', 'otaservice')->find();
        if ($res) {
            $ota_info = unserialize($res['value']);
        }
        $ota_info['updatekey'] = func_substr_replace($ota_info['updatekey'], '*', 5, 12);
        $this->assign('ota_info', $ota_info);
        return $this->fetch();
    }

    public function updateota()
    {
        if ($this->request->isPost()) {
            $ota_config = $this->request->post();
            if(strpos($ota_config['updatekey'],"*")!==false){
                return json(array('code' => 0, 'msg' => '不能含有*号'));
            }
            $data['value'] = serialize($ota_config);
            $res = Db::name('system')->where('name', 'otaservice')->find();
            if ($res) {
                $reslut = Db::name('system')->where('name', 'otaservice')->update($data);
            } else {
                $data['name'] = 'otaservice';
                $reslut = Db::name('system')->insert($data);
            }
            if ($reslut) {
                return json(array('code' => 200, 'msg' => '保存成功'));
            } else {
                return json(array('code' => 0, 'msg' => '提交失败'));
            }
        }
    }


    public function qiniu()
    {
        $qiniu_config = Db::name('system')->field('value')->where('name', 'qiniu')->find();

        $qiniu_config = unserialize($qiniu_config['value']);
        $this->assign('qiniu', $qiniu_config);
        return $this->fetch('qiniu');
    }

    public function updateqiniu()
    {
        if ($this->request->isPost()) {
            $site_config = $this->request->post('qiniu/a');
            $data['value'] = serialize($site_config);

            if (Db::name('system')->where('name', 'qiniu')->update($data) !== false) {
                return json(array('code' => 200, 'msg' => '提交成功'));
            } else {
                return json(array('code' => 200, 'msg' => '提交失败'));
            }
        }

    }

    public function mail()
    {
        $sms_config = Db::name('system')->field('value')->where('name', 'mail')->find();

        $sms_config = unserialize($sms_config['value']);
        $this->assign('mail', $sms_config);
        return $this->fetch('mail');
    }

    public function updatemail()
    {
        if ($this->request->isPost()) {
            $site_config = $this->request->post('mail/a');
            $data['value'] = serialize($site_config);

            $reslut = 0;
            $res = Db::name('system')->where('name', 'mail')->find();
            if ($res) {
                $reslut = Db::name('system')->where('name', 'mail')->update($data);
            } else {
                $data['name'] = 'mail';
                $reslut = Db::name('system')->insert($data);
            }
            if ($reslut) {
                return json(array('code' => 200, 'msg' => '保存成功'));
            } else {
                return json(array('code' => 200, 'msg' => '提交失败'));
            }
        }

    }

    public function sms()
    {
        $sms_config = Db::name('system')->field('value')->where('name', 'sms')->find();

        $sms_config = unserialize($sms_config['value']);
        $this->assign('sms', $sms_config);
        return $this->fetch('sms');
    }

    public function updatesms()
    {
        if ($this->request->isPost()) {
            $site_config = $this->request->post('sms/a');
            $data['value'] = serialize($site_config);

            $reslut = 0;
            $res = Db::name('system')->where('name', 'sms')->find();
            if ($res) {
                $reslut = Db::name('system')->where('name', 'sms')->update($data);
            } else {
                $data['name'] = 'sms';
                $reslut = Db::name('system')->insert($data);
            }
            if ($reslut) {
                return json(array('code' => 200, 'msg' => '保存成功'));
            } else {
                return json(array('code' => 200, 'msg' => '提交失败'));
            }
        }
    }
    public function pay()
    {
        $config = Db::name('system')->field('value')->where('name', 'pay')->find();
        $config = unserialize($config['value']);
        $this->assign('config', $config);
        return $this->fetch('pay');
    }

    public function updatepay()
    {
        if ($this->request->isPost()) {
            $config = $this->request->post('config/a');
            $data['value'] = serialize($config);


            $res = Db::name('system')->where('name', 'pay')->find();
            if ($res) {
                $reslut = Db::name('system')->where('name', 'pay')->update($data);
            } else {
                $data['name'] = 'pay';
                $reslut = Db::name('system')->insert($data);
            }
            if ($reslut) {
                return json(array('code' => 200, 'msg' => '保存成功'));
            } else {
                return json(array('code' => 200, 'msg' => '提交失败'));
            }
        }
    }

    public function qqlogin()
    {
        $config = Db::name('system')->field('value')->where('name', 'qqlogin')->find();

        $config = unserialize($config['value']);
        $this->assign('config', $config);
        return $this->fetch('qqlogin');
    }

    public function updateqqlogin()
    {
        if ($this->request->isPost()) {
            $site_config = $this->request->post('config/a');
            $data['value'] = serialize($site_config);

            $reslut = 0;
            $res = Db::name('system')->where('name', 'qqlogin')->find();
            if ($res) {
                $reslut = Db::name('system')->where('name', 'qqlogin')->update($data);
            } else {
                $data['name'] = 'qqlogin';
                $reslut = Db::name('system')->insert($data);
            }
            if ($reslut) {
                return json(array('code' => 200, 'msg' => '保存成功'));
            } else {
                return json(array('code' => 200, 'msg' => '提交失败'));
            }
        }
    }
}